<?php
// http://phpdev.ro/how-to-integrate-uploadify-in-zend-framework-1-9-6.html
// http://www.cagintranet.com/archive/how-to-implement-uploadify-into-an-application/
/**
 * This file is part of Task Agile.
 * Task Agile is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License.
 * Task Agile is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Task Agile.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category   TaskAgile
 * @package    Controller
 * @copyright  Copyright (c) 20010-2010 Estrada Virtual (http://estradavirtual.com.br)
 * @license    http://www.gnu.org/licenses/gpl.txt GPLv3
 * @version    $Id$
*/
class FileController extends Tev_Controller_Action
{
    public function uploadAction()
    {
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        if (!$idTask = $this->_getParam('id', false)) {
            return;
        }

        if (empty($idTask)) {
            return;
        }

        if (!$idUser = $this->_getParam('idUser', false)) {
            return;
        }

        if (empty($idUser)) {
            return;
        }

        if (!empty($_FILES)) {
            $taskUpload = new TaskUpload();
            $taskUpload->upload($idUser, $idTask, $_FILES ['Filedata']['name'], $_FILES ['Filedata']['tmp_name']);
        }
    }

    public function checkByTaskAction()
    {
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);

        $fileArray = array();
        foreach ($_POST as $key => $value) {
            if ($key != 'folder') {
                if (file_exists($_SERVER['DOCUMENT_ROOT'] . $_POST['folder'] . '/' . $value)) {
                    $fileArray[$key] = $value;
                }
            }
        }
        echo json_encode($fileArray);
    }

    public function listByTaskAction()
    {
        if ($this->getRequest()->isPost()) {
            $this->_helper->layout()->disableLayout();
        }

        if (!$idTask = $this->_getParam('id', false)) {
            return;
        }

        if (empty($idTask)) {
            return;
        }

        $taskUpload = new TaskUpload();
        $this->view->rowSet = $taskUpload->findByTask($idTask);
    }

    public function getAction()
    {
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        if (!$idTaskUpload = $this->_getParam('id', false)) {
            return;
        }
        $taskUpload = new TaskUpload();
        if (!$rowTaskUpload = $taskUpload->find($idTaskUpload)->current()) {
            $this->_redirect('index');
        }
        $pathFile = $taskUpload->getPathFile($rowTaskUpload);

        if (file_exists($pathFile)) {
            $this->_forceDownload($pathFile);
        }
    }

    private function _forceDownload($file)
    {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename=' . basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        exit;
    }
}
